package me.sdevil507.biz.fruit.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.fruit.dto.*;
import me.sdevil507.biz.fruit.mapper.FruitMapper;
import me.sdevil507.biz.fruit.po.*;
import me.sdevil507.biz.fruit.service.FruitService;
import me.sdevil507.org.service.OrgRoleService;
import me.sdevil507.services.dto.ResultDTO;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.supports.shiro.helper.ServiceAccountHelper;
import me.sdevil507.supports.status.ApiStatusCode;
import me.sdevil507.supports.status.ComConstant;
import me.sdevil507.systemAssist.Constant.SystemAssistConstant;
import me.sdevil507.systemAssist.systemLog.annotation.OperLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @program: tips_back_end
 * @description: 成果消费端
 **/
@RestController
@RequestMapping("/api/services/fruit")
@Slf4j
public class FruitServicesController {

    @Autowired
    private FruitService fruitService;
    @Autowired
    private OrgRoleService orgRoleService;
    @Autowired
    private FruitMapper fruitMapper;

    /**
     * 主页查询
     * @param fruitParamsDTO
     * @return
     */
    @OperLog(operTerminal = SystemAssistConstant.systemAssistTerminal.SERVICES, operClassify = SystemAssistConstant.systemAssistClassify.FRUIT, operModule = SystemAssistConstant.systemAssistModule.FRUIT_SEARCH, operDesc = "")
    @PostMapping("/v1/readAllServicesFruitByPage")
    public ApiResultDTO readAllServicesFruitByPage(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            if(CollectionUtils.isEmpty(fruitParamsDTO.getSearchDto().getOrgIdList())){
                //设置机构id列表
                fruitParamsDTO.getSearchDto().setOrgIdList(OrgAccountHelper.getOrgIdList());
            }

            Page<ResultServicesHomeListFruitDTO> pagePos = fruitService.readAllServicesFruitByPage(fruitParamsDTO);

            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }

    /**
     * 专报查询
     * @param fruitParamsDTO
     * @return
     */
    @PostMapping("/v1/readAllServicesSpecialFruitByPage")
    public ApiResultDTO readAllServicesSpecialFruitByPage(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            //设置机构id列表
            fruitParamsDTO.getSearchDto().setOrgIdList(OrgAccountHelper.getOrgIdList());

            Page<ResultServicesHomeListFruitDTO> pagePos = fruitService.readAllServicesFruitByPage(fruitParamsDTO);

            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }

    /**
     * 词云基础数据查询
     * @param fruitParamsDTO
     * @return
     */
    @PostMapping("/v1/readServicesFruitWords")
    public ApiResultDTO readAllServicesFruitList(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            //设置机构id列表
            fruitParamsDTO.getSearchDto().setOrgIdList(OrgAccountHelper.getOrgIdList());

            System.out.println("111==="+new Date());
            List<ResultServicesHomeListFruitDTO> resultList = fruitService.readAllServicesFruitList(fruitParamsDTO);
            //拼接词云基础数据
            List<String> wordLists = new ArrayList<>();
            resultList.forEach(dto->{
                wordLists.add(dto.getName());
                wordLists.add(dto.getSummary());
                wordLists.add(dto.getRemark());
            });
            System.out.println("222==="+new Date());
            //生成词云
            List<ResultDTO> wordResultList = null;
            System.out.println("333==="+new Date());

            apiResultDto = ApiResultGenerator.create("0", "词云基础数据获取成功!", wordResultList);
        } catch (Exception e) {
            log.error("词云基础数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "词云基础数据获取失败!");
        }
        return apiResultDto;
    }


    /**
     * 获取单个成果
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/v1/getFruitById")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO getFruitById(@RequestBody FruitDTO dto) {
        ApiResultDTO apiResultDTO;
        try {
            FruitPO fruitPo = fruitService.findOneById(dto.getId());

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), fruitPo);
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }


    /**
     * 获取单个成果的作者信息
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/v1/getFruitAuthorById")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO getFruitAuthorById(@RequestBody FruitDTO dto) {
        ApiResultDTO apiResultDTO;
        try {
            List<FruitAuthorPO> authorPOList = fruitService.getFruitAuthorById(dto.getId());

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), authorPOList);
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }

    /**
     * 获取成果及来源单位
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/v1/getFruitInfoAndOrgInfoById")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO getFruitInfoAndOrgInfoById(@RequestBody FruitDTO dto) {
        ApiResultDTO apiResultDTO;
        try {
            ResultServicesHomeListFruitDTO result = fruitService.getFruitInfoAndOrgInfoById(dto.getId());

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), result);
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }

    /**
     * 采纳专报
     *
     * @return 执行反馈
     */
    @PostMapping("/v1/acceptSpecial")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO acceptSpecial(@RequestBody FruitSubPO fruitSubPO) {
        ApiResultDTO apiResultDTO;
        try {
            FruitPO fruitPo = fruitService.findOneById(fruitSubPO.getFruitId());
            fruitService.acceptSpecial(fruitPo, fruitSubPO);

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }

    /**
     * 拒绝采纳
     *
     * @return 执行反馈
     */
    @PostMapping("/v1/refuseSpecial")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO refuseSpecial(@RequestBody FruitDTO fruitDTO) {
        ApiResultDTO apiResultDTO;
        try {
            FruitPO fruitPo = fruitService.findOneById(fruitDTO.getId());
            fruitService.refuseSpecial(fruitPo);

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            log.error(" 拒绝采纳失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }


    /**
     * 撤销拒绝
     *
     * @return 执行反馈
     */
    @PostMapping("/v1/revocationSpecial")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO revocationSpecial(@RequestBody FruitDTO fruitDTO) {
        ApiResultDTO apiResultDTO;
        try {
            FruitPO fruitPo = fruitService.findOneById(fruitDTO.getId());
            fruitService.revocationSpecial(fruitPo);

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            log.error("撤销失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }


    /**
     * 专报录入批示
     *
     * @return 执行反馈
     */
    @PostMapping("/v1/inputPsSpecial")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO inputPsSpecial(@RequestBody FruitLeaderPO fruitLeaderPO) {
        ApiResultDTO apiResultDTO;
        try {
            fruitService.inputPsSpecial(fruitLeaderPO);

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }


    /**
     * 删除批示
     *
     * @return 执行反馈
     */
    @PostMapping("/v1/deletePsSpecial")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO deletePsSpecial(@RequestBody FruitLeaderPO fruitLeaderPO) {
        ApiResultDTO apiResultDTO;
        // 执行逻辑删除
        try {
            fruitService.deletePsSpecial(fruitLeaderPO);

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }


    /**
     * 专报反馈列表
     *
     * @return 执行反馈
     */
    @PostMapping("/v1/getFeedBackListByFruitId")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO getFeedBackListByFruitId(@RequestBody FruitFeedBackPO fruitFeedBackPO) {
        ApiResultDTO apiResultDTO;
        try {
            List<FruitFeedBackPO> fruitFeedBackPOS = fruitService.getOneFruitFeedBackByFruitId(fruitFeedBackPO);

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), fruitFeedBackPOS);
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取专报反馈列表失败");
        }
        return apiResultDTO;
    }



    /**
     * 新增专报反馈
     *
     * @return 执行反馈
     */
    @PostMapping("/v1/inputSpecialFk")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO inputSpecialFk(@RequestBody FruitFeedBackPO fruitFeedBackPO) {
        ApiResultDTO apiResultDTO;
        try {
            fruitFeedBackPO.setType("2");
            fruitFeedBackPO.setIsLook(false);
            fruitFeedBackPO.setSourceName(ComConstant.specialDeptName.JSSWXCB);
            fruitService.inputSpecialFk(fruitFeedBackPO);

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }

    /**
     * 删除专报反馈
     *
     * @return 执行反馈
     */
    @PostMapping("/v1/deleteSpecialFk")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO deleteSpecialFk(@RequestBody FruitFeedBackPO fruitFeedBackPO) {
        ApiResultDTO apiResultDTO;
        try {
            fruitService.deleteSpecialFk(fruitFeedBackPO);

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }



    /**
     *
     * 智库管理--智库详情--获取所有成果的信息
     *
     * @return
     */
    @PostMapping("/getAllFruitOfPageByOrgId")
    public ApiResultDTO getAllFruitOfPageByOrgId(@RequestBody FruitParamsDTO dto) {

        Page<ResultHomeListFruitDTO> resultDTOList = fruitService.getAllFruitOfPageByOrgId(dto);

        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), resultDTOList);
    }

    /**
     * 获取信息报送数据
     * 1、一定是智库本身自己主办的活动(出席会议)
     * 2、接受采访或者文章，可以过滤
     * 提供报道的链接
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/v1/readAllReportInformationByPage")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO readAllReportInformationByPage(@RequestBody FruitParamsDTO dto) {
        ApiResultDTO apiResultDTO;
        try {
            if(dto.getSearchDto().getOrgId() != null){
                Set<Long> orgIdList = new HashSet<>();
                orgIdList.add(dto.getSearchDto().getOrgId());
                dto.getSearchDto().setOrgIdList(orgIdList);
            }else{
                dto.getSearchDto().setOrgIdList(OrgAccountHelper.getOrgIdList());
            }

            Page<ResultServicesReportInfoDTO> result = fruitService.readAllReportInformationByPage(dto);

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), result);
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }
}
